package Tree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class item111 {
}
class Solution111 {

    public static int  minDepth(TreeNode root) {
        if(root==null) return 0;
        int depth =1;
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);
        while (!queue.isEmpty())
        {
            int size = queue.size();
            for (int i = 0; i <size ; i++) {
                TreeNode poll = queue.poll();
                if(poll.left==null&&poll.right==null)
                    return depth;
                if(poll.left!=null)
                {
                    queue.add(poll.left);
                }
                if(poll.right!=null)
                {
                    queue.add(poll.right);
                }
            }
            depth++;
        }
       return depth;
    }




    public static void main(String[] args) {
        TreeNode root = new TreeNode(1);
        TreeNode  node1 = new TreeNode(2);
        TreeNode node2 = new TreeNode(3);
        TreeNode node3 = new TreeNode(4);
        TreeNode node4 = new TreeNode(5);
        TreeNode node5 = new TreeNode(6);
        TreeNode node6 = new TreeNode(7);
        root.right = node1;
        node1.right = node2;
        node2.right = node3;
        node3.right = node4;
        minDepth(root);

    }
}